package lv.bond.science.nnstudio.ui.errorgraph;

import java.awt.Dimension;
import java.awt.Font;
import java.util.Iterator;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import lv.bond.science.nnstudio.model.ImagesBean;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.internal.activities.ws.ImageBindingRegistry;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class ErrorGraphView extends ViewPart {

	public static final String ID = "lv.bond.science.nnstudio.ui.errorgraph.ErrorGraphView"; 
	// Needs to be whatever is mentioned in plugin.xml

	private Composite top = null;
	private Composite composite = null;
	private float[] testData = {0.2f, 0.3f, 0.4f};
	
	private static XYSeriesCollection xySeriesCollection = 
		new XYSeriesCollection();

	@Override
	public void createPartControl(Composite parent) {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		top = new Composite(parent, SWT.NONE);
		top.setLayout(gridLayout);
		createComposite();
		
	}

	@Override
	public void setFocus() {
		composite.setFocus();
	}

	/**
	 * This method initializes composite	
	 *
	 */
	private void createComposite() {
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.verticalAlignment = GridData.FILL;
		composite = new Composite(top, SWT.EMBEDDED);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(gridData);

		embedJPanelInto(composite);
	    
	}
	
	private void embedJPanelInto(Composite container) {
		java.awt.Frame graphFrame = 
	    	SWT_AWT.new_Frame(container);
	    java.awt.Panel panel = 
	    	new java.awt.Panel(new java.awt.BorderLayout());
	    graphFrame.add(panel);
	    JScrollPane scrollPane = new JScrollPane(
	    		NormalDistributionDemo("Normal Distribution Demo"));
	    panel.add(scrollPane);
		
	}

	public JPanel NormalDistributionDemo(String s) {
        JPanel jpanel = errorGraphPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        
        return jpanel;
    }
    
    /*private static void fillDataset() { 
    	Iterator iter = ErrorGraphView.xySeriesCollection.getSeries().iterator();
    	while (iter.hasNext()) {
			XYSeries series = (XYSeries) iter.next();
			
			Random rnd = new Random();
			int seriesSize = series.getItemCount();
	        for(int i = seriesSize; i < seriesSize + 10; i++ ) {
	            float x = i;
	            float y = rnd.nextFloat();
	            series.add(x, y);
	        }
			
		}
	}*/
    
    public static void clearDataset() {
    	Iterator iter = ErrorGraphView
    						.xySeriesCollection
    						.getSeries()
    						.iterator();
    	while (iter.hasNext()) {
			XYSeries series = (XYSeries) iter.next();
			series.clear();
		}
    }
    
    public static void appendToDataSet(float[] data) {
    	XYSeries series =ErrorGraphView
							.xySeriesCollection
							.getSeries(0);
    	
    	int seriesSize = series.getItemCount();
    	for (int i = seriesSize; i < seriesSize + data.length; i++) {
			float x = i;
			float y = data[i - seriesSize];
			series.add(x, y);
		}
    }
    
    public JPanel errorGraphPanel() {
        XYSeriesCollection xyseriescollection = createDataset();
        JFreeChart jfreechart = createChart(xyseriescollection);
        ChartPanel chartpanel = new ChartPanel(jfreechart);
        chartpanel.setPreferredSize(new Dimension(360, 500));
        return chartpanel;
    }

    private static XYSeriesCollection createDataset()
    {
        XYSeriesCollection xyseriescollection = //new XYSeriesCollection();
        	ErrorGraphView.xySeriesCollection;

        XYSeries xyseries = new XYSeries("Error", true, false);
        //XYSeries xyseries1 = new XYSeries("D2", true, false);
        /*Random rnd = new Random();
        for(int i = 0; i < 10; i++ ) {
            float x = i;
            float y1 = rnd.nextFloat();
            float y2 = rnd.nextFloat();
            xyseries.add(x, y1);
            xyseries1.add(x, y2);
        }*/

        xyseriescollection.addSeries(xyseries);
        //xyseriescollection.addSeries(xyseries1);

        return xyseriescollection;
    }

    private static JFreeChart createChart(XYDataset xydataset) {
        JFreeChart jfreechart = ChartFactory.createXYLineChart(null, "Epochs", "Error", xydataset, PlotOrientation.VERTICAL, true, true, false);
        TextTitle texttitle = new TextTitle("Network error", new Font("SansSerif", 1, 14));
        jfreechart.addSubtitle(texttitle);
        XYPlot xyplot = jfreechart.getXYPlot();
        NumberAxis numberaxis = (NumberAxis)xyplot.getDomainAxis();
        numberaxis.setUpperMargin(0.12D);
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        NumberAxis numberaxis1 = (NumberAxis)xyplot.getRangeAxis();
        numberaxis1.setAutoRangeIncludesZero(false);

        return jfreechart;
    }

}
